package ramuanajaib;

import java.util.Vector;

public class Location implements GameConstant {

    private Vector<Ingredients> ingredients;
    private Vector<Integer> ingredients_needed;
    private Vector<Integer> qty_needed;
    private Patient patient;
    private int score;
    private int chapter;
    private int chances;
    private int level;
    private int star;
    private Potion potion;

    public Location() {
        ingredients = new Vector<Ingredients>();
        ingredients_needed = new Vector<Integer>();
        qty_needed = new Vector<Integer>();
        patient = new Patient();
        potion = new Potion();
        chapter = 1;
    }

    public void addIngredient(Ingredients ingredient) {
        ingredients.add(ingredient);
    }

    /* Fungsi ini buat masukin bahan yang dipilih ke bahan ramuan */
    public void addPotionIngredient(int category, int quantity) {
        potion.addIngredient(new Ingredients(category), quantity);
    }

    public void addRequirement(int category, int quantity) {
        ingredients_needed.add(category);
        qty_needed.add(quantity);
    }

    public void mixPotion() {
        boolean match = true;
        boolean qty_match = true;
        for (int i = 0; i < ingredients_needed.size(); i++) {
            int value = potion.IngredientMatch(ingredients_needed.get(i), qty_needed.get(i));

            if (value == -1) {
                match = false;
            } else if (value != 0) {
                qty_match = false;
            }
        }

        if (!match) {
            potion.setEffect(POT_EFFECT_FAILURE);
        } else {
            if (qty_match) {
                potion.setEffect(POT_EFFECT_SUCCESS);
            } else {
                potion.setEffect(POT_EFFECT_GREAT);
            }
        }
    }

    public void countScore() {
        for (int i = 0; i < ingredients_needed.size(); i++) {

            int value = potion.IngredientMatch(ingredients_needed.get(i), qty_needed.get(i));
            if (value != -1) {
                score += 100;
                if (value == 0) {
                    score += 25;
                }
            }
        }
    }

    public void countStar(){
        int max_score = ingredients_needed.size() * 125;
        if (score > max_score * 0.8){
            star = 3;
        } else if (score > max_score * 0.5){
            star = 2;
        } else {
            star = 1;
        }
    }

    public void checkEndCondition() {
        if (potion.getEffect() == POT_EFFECT_FAILURE) {
            if (chances == 0) {
                //Game Over
            } else {
                --chances;
            }
        } else {
            //Next Chapter
            if (chapter == 5) {
                if (level == 3) {
                    //Tamat
                } else {
                    chapter = 1;
                    ++level;
                }
            } else {
                ++chapter;
            }
            countScore();
            countStar();
        }
    }

    public int getPotionEffect() {
        return potion.getEffect();
    }

    public int getChapter() {
        return chapter;
    }

    public void setChapter(int chapter) {
        this.chapter = chapter;
    }

    public Vector<Ingredients> getIngredients() {
        return ingredients;
    }

    public void setIngredients(Vector<Ingredients> ingredients) {
        this.ingredients = ingredients;
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }

    public Potion getPotion() {
        return potion;
    }

    public void setPotion(Potion potion) {
        this.potion = potion;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getStar() {
        return star;
    }

    public void setStar(int star) {
        this.star = star;
    }
    
}
